package com.dms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.QueryPageParam;
import com.dms.common.Result;
import com.dms.entity.Menu;
import com.dms.entity.Suser;
import com.dms.service.MenuService;
import com.dms.service.SuserService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;



@RestController
@RequestMapping(value = "/suser")
public class Hellocontroller {
    @GetMapping
    public String hello(){
        return "hello wms";
    }
    //注入SuserService
    @Autowired
    private SuserService suserService;
    @Autowired
    private MenuService menuService;
    //Get Mapper（"/list"）查询suser表的数据据信息
    @GetMapping("/list")
    public List<Suser> list(){
//        return  suserService.list();
        //实现按要求执行sql语句
        return  suserService.listAll();
    }
    @GetMapping("/findByNo")
public Result findByNo(@RequestParam String no){
    List list= suserService.lambdaQuery().eq(Suser::getNo,no).list();
    return list.size()>0?Result.suc(list):Result.fail();
}
//登录
    @PostMapping("/login")
    public Result login(@RequestBody Suser suser){
      List list= suserService.lambdaQuery().eq(Suser::getNo,suser.getNo())
              .eq(Suser::getPassword,suser.getPassword()).list();

      if(list.size()>0){
          Suser suser1=(Suser)list.get(0);
          List menuList=menuService.lambdaQuery().like(Menu::getMenuright,suser1.getRoleId()).list();
          HashMap res=new HashMap();
          res.put("suser",suser1);
          res.put("menu",menuList);
//          System.out.println(menuList);
          return Result.suc(res);
      }
        return Result.fail();
    }

//修改
    @PostMapping("/update")
    public Result update(@RequestBody Suser suser){
        return suserService.updateById(suser)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam int id){
        return suserService.removeById(id)?Result.suc():Result.fail();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Suser suser){

        return suserService.save(suser)?Result.suc():Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Suser suser){
        return suserService.updateById(suser);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody Suser suser){
        return suserService.saveOrUpdate(suser);
    }
    //删除
    @GetMapping("/delte")
    public boolean delete(Integer id){
        return suserService.removeById(id);
    }
    //模糊查询或精确查询
    @PostMapping("/listP")
    public Result listp(@RequestBody Suser suser){
        LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
            if(StringUtils.isNotBlank(suser.getName())) {
                lambdaQueryWrapper.like(Suser::getName, suser.getName());
            }
        System.out.println(suser.getName());
            //return suserService.list(lambdaQueryWrapper);
//        System.out.println(suserService.list(lambdaQueryWrapper));
        return Result.suc(suserService.list(lambdaQueryWrapper));
    }
    //精准查询
    @PostMapping("/listt")
    public List<Suser> listt(@RequestBody Suser suser){
        LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Suser::getName,suser.getName());
        return suserService.list(lambdaQueryWrapper);
    }

    //精准查询分页
    @PostMapping("/listpage")
    public List<Suser> listpage(@RequestBody QueryPageParam query){
     /*   LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Suser::getName,suser.getName());
        return suserService.list(lambdaQueryWrapper);*/

        HashMap param =query.getParam();
        String name=(String) param.get("name");
        /*        System.out.println(query);
        System.out.println("num=="+query.getPageNum());
        System.out.println("size"+query.getPageSize());
        */
        Page<Suser> page=new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Suser::getName,name);
       IPage result= suserService.page(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }
    //并设置wramapper分页
    @PostMapping("/listpagec")
    public List<Suser> listpagec(@RequestBody QueryPageParam query){
        HashMap param =query.getParam();
        String name=(String) param.get("name");
        Page<Suser> page=new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        /*
        LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Suser::getName,name);*/


        IPage result= suserService.pageC(page);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }
    //自写sql并设置wramapper分页
    @PostMapping("/listpagecc")
    public List<Suser> listpagecc(@RequestBody QueryPageParam query){
        HashMap param =query.getParam();
        String name=(String) param.get("name");
        Page<Suser> page=new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Suser::getName,name);


        IPage result= suserService.pageCC(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }
    //精准查询并分页
    @PostMapping("/listpagecc1")
    public Result listpagecc1(@RequestBody QueryPageParam query){
        HashMap param =query.getParam();
        String name=(String) param.get("name");
        String roleId=(String) param.get("roleId");


        Page<Suser> page=new Page();

        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Suser> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Suser::getName, name);
        }
            if(StringUtils.isNotBlank(roleId)){
                lambdaQueryWrapper.eq(Suser::getRoleId,roleId);
            }

        IPage result= suserService.pageCC(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        System.out.println(result.getRecords());
        return Result.suc(result.getRecords(),result.getTotal());
    }
}
